package com.pine.manager.pineframework.trigger.impl;

import com.pine.common.beans.exception.BusinessException;
import com.pine.common.beans.exception.ExceptionCodeEnum;
import com.pine.common.beans.pineframework.request.SysDictItemSaveOrUpdateRequest;
import com.pine.common.trigger.TriggerContext;
import com.pine.dao.pineframework.entity.SysDict;
import com.pine.dao.pineframework.entity.SysDictItem;
import com.pine.manager.core.TriggerTypeEnum;
import com.pine.manager.pineframework.bo.SysDictBo;
import com.pine.manager.pineframework.bo.SysDictItemBo;
import com.pine.manager.pineframework.query.SysDictItemQuery;
import com.pine.manager.pineframework.trigger.AbstractTriggerTemplateAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * <p>
 * 系统字典项表新增或修改数据 触发器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-16
 */
@Component
@Slf4j
public class SysDictItemSaveOrUpdateTrigger extends AbstractTriggerTemplateAdapter<SysDictItemSaveOrUpdateRequest, Boolean> {

    private boolean isSave;

    private SysDictItemBo oldSysDictItemBo;

    public SysDictItemSaveOrUpdateTrigger() {
        super(TriggerTypeEnum.SYS_DICT_ITEM_SAVE_OR_UPDATE);
    }

    @Override
    protected void before(TriggerContext<SysDictItemSaveOrUpdateRequest> triggerContext) {
        // 获取请求参数
        SysDictItemSaveOrUpdateRequest request = triggerContext.getTriggerRequest();
        isSave = Objects.isNull(request.getId());

        // 校验字典是否存在
        SysDictBo sysDictBo = sysDictManager.getOneByUniqueKey(request.getDictId(), SysDict::getId);
        if (Objects.isNull(sysDictBo)) {
            throw new BusinessException(ExceptionCodeEnum.DICT_NOT_EXIST);
        }

        // 尝试查询旧的字典项数据
        SysDictItemQuery query = new SysDictItemQuery();
        query.setId(request.getId());
        query.setDictId(request.getDictId());
        if (isSave) {
            query.setName(request.getName());
        }

        oldSysDictItemBo = sysDictItemManager.getOne(query);

        // 校验
        if (isSave) {
            checkOnSave();
        } else {
            checkOnUpdate();
        }
    }

    /**
     * 触发
     *
     * @param triggerContext 触发上下文
     * @return 触发结果
     */
    @Override
    public Boolean trigger(TriggerContext<SysDictItemSaveOrUpdateRequest> triggerContext) {
        // 获取请求参数
        SysDictItemSaveOrUpdateRequest request = triggerContext.getTriggerRequest();

        // 转换为业务对象
        SysDictItemBo newSysDictBo = sysDictItemConvertor.saveRequestToEntityBo(request);

        // 转换为实体
        SysDictItem sysDictItem = sysDictItemConvertor.entityBoToEntity(newSysDictBo);

        // 新增
        if (isSave) {
            return sysDictItemManager.save(sysDictItem);
        }
        // 修改
        return sysDictItemManager.updateById(sysDictItem);
    }


    private void checkOnUpdate() {
        // 校验字典项ID是否存在
        if (Objects.isNull(oldSysDictItemBo)) {
            throw new BusinessException(ExceptionCodeEnum.DICT_ITEM_NOT_EXIST);
        }
    }

    private void checkOnSave() {
        // 校验字典项name是否重复
        if (Objects.nonNull(oldSysDictItemBo)) {
            throw new BusinessException(ExceptionCodeEnum.DICT_ITEM_EXIST);
        }
    }
}
